package com.xms.schema.service.attribute;

import com.xms.core.exception.XmsException;
import com.xms.core.module.ModuleCollection;
import com.xms.dependency.XmsDependencyException;
import com.xms.dependency.model.DependentDescriptor;
import com.xms.dependency.service.IDependencyBatchBuilder;
import com.xms.dependency.service.IDependencyDeleterService;
import com.xms.dependency.service.IDependencyUpdaterService;
import com.xms.schema.core.AttributeDefaults;
import com.xms.schema.core.EntityDefaults;
import com.xms.schema.core.OptionSetDefaults;
import com.xms.schema.entity.Attribute;
import com.xms.schema.extension.AttributeExt;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.SQLException;
import java.util.UUID;

/**
 * <p>
 * AttributeDependency
 * </p>
 *
 * @author migo
 * @since 2021/10/24
 */
@Service
public class AttributeDependency implements IAttributeDependency {
    private IAttributeFinderService _attributeFinderService;
    private IDependencyBatchBuilder _dependencyBatchBuilder;
    private IDependencyUpdaterService _dependencyUpdaterService;
    private IDependencyDeleterService _dependencyDeleterService;

    @Autowired
    public void AttributeDependency(IAttributeFinderService attributeFinderService,
                                    IDependencyBatchBuilder dependencyBatchBuilder,
                                    IDependencyUpdaterService dependencyUpdaterService,
                                    IDependencyDeleterService dependencyDeleterService) {
        _attributeFinderService = attributeFinderService;
        _dependencyBatchBuilder = dependencyBatchBuilder;
        _dependencyUpdaterService = dependencyUpdaterService;
        _dependencyDeleterService = dependencyDeleterService;
    }

    @Override
    public DependentDescriptor getDependent(UUID dependentId) {
        Attribute result = _attributeFinderService.getById(dependentId);
        return result != null ? new DependentDescriptor() {
            {
                name = result.getLocalizedname() + "(" + result.getEntityname() + ")";
            }
        } : null;
    }

    @Override
    public int getComponentType() {
        return ModuleCollection.getIdentity(AttributeDefaults.MODULE_NAME);
    }

    @Override
    public boolean create(Attribute entity) throws XmsException, SQLException {
        //引用类型
        if (AttributeExt.typeIsRef(entity)) {
            Attribute referenced = _attributeFinderService.getPrimaryKey(entity.getReferencedentityid());
            //依赖于被引用实体
            _dependencyBatchBuilder.append(AttributeDefaults.MODULE_NAME, entity.getAttributeid(), EntityDefaults.MODULE_NAME, entity.getReferencedentityid())
                    .append(AttributeDefaults.MODULE_NAME, entity.getAttributeid(), AttributeDefaults.MODULE_NAME, referenced.getAttributeid())
                    .save();
        }
        //选项类型
        else if (AttributeExt.typeIsPickList(entity)) {
            if (entity.getOptionset() != null && entity.getOptionset().getIspublic()) {
                //依赖于公共选项集
                _dependencyBatchBuilder.append(AttributeDefaults.MODULE_NAME, entity.getAttributeid(), OptionSetDefaults.MODULE_NAME, entity.getOptionsetid())
                        .save();
            }
        }
        return true;
    }

    @Override
    public boolean delete(UUID... id) throws SQLException, XmsDependencyException {
        return _dependencyDeleterService.deleteByDependentId(AttributeDefaults.MODULE_NAME, id);
    }

    @Override
    public boolean update(Attribute entity) throws XmsException, SQLException {
        //引用类型
        if (AttributeExt.typeIsRef(entity)) {
            Attribute referenced = _attributeFinderService.getPrimaryKey(entity.getReferencedentityid());
            _dependencyUpdaterService.update(AttributeDefaults.MODULE_NAME, entity.getAttributeid(), EntityDefaults.MODULE_NAME, entity.getReferencedentityid());
            _dependencyUpdaterService.update(AttributeDefaults.MODULE_NAME, entity.getAttributeid(), AttributeDefaults.MODULE_NAME, referenced.getAttributeid());
        }
        //选项类型
        else if (AttributeExt.typeIsPickList(entity)) {
            if (entity.getOptionset() != null && entity.getOptionset().getIspublic()) {
                //依赖于公共选项集
                _dependencyUpdaterService.update(AttributeDefaults.MODULE_NAME, entity.getAttributeid(), OptionSetDefaults.MODULE_NAME, entity.getOptionsetid());
            }
        }
        return true;
    }
}
